﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.BaoJia
{

    public partial class Add : System.Web.UI.Page
    {
        private BaojiaManager objBaojiaManager = new BaojiaManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager(); 
        int workParentId;
        private static string no;

        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            if (!IsPostBack)
            {
                try
                {
                    Baojia objBaojia = objBaojiaManager.GetBaojiaInfo(workParentId);
                    if (objBaojia != null)
                    {
                        no = objBaojia.Number;
                        txtNo.Text = objBaojia.Number;
                        foreach (var item in Enum.GetValues(typeof(Customer)))
                        {
                            if(objBaojia.Customer==item.ToString())
                            {
                                ddlCustomer.SelectedValue = objBaojia.Customer;
                                otherCustmoer.Style.Value = "display:none";
                                break;
                            }
                            else
                            {
                                ddlCustomer.SelectedValue = "其他";
                                txtCustomer.Text = objBaojia.Customer;
                                otherCustmoer.Style.Value = "";
                            }
                        }
                        
                        //txtCustomer.Text = objBaojia.Customer;
                        txtRequirement.Text = objBaojia.Requirement.ToString();
                        txtSales.Text = objBaojia.Sales.ToString();
                        ddlClass.SelectedValue = objBaojia.Class;
                        ddlType.SelectedValue = objBaojia.CostType;
                        ddlSuccess.SelectedValue = objBaojia.Success.ToString();
                    }
                    else
                    {
                        no = objBaojiaManager.GetNewNumber();
                        txtNo.Text = "";
                        txtCustomer.Text = "";
                        txtRequirement.Text = "";
                        txtSales.Text = "";
                        ddlClass.SelectedIndex = 0;
                        ddlType.SelectedIndex = 0;
                        ddlSuccess.SelectedIndex = 0;
                        ddlCustomer.SelectedIndex = 0;
                        otherCustmoer.Style.Value = "display:none";
                    }
                    //权限设置
                    //int ieCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 2, 1);
                    //if(ieCount>0)
                    //{
                    //    ddlType.Enabled = true;
                    //    txtNo.Enabled = true;
                    //    //ddlCustomer.Enabled = true;
                    //    //txtCustomer.Enabled = true;
                    //}
                    int saleCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 3, 1) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 3, 4);
                    if (saleCount > 0)
                    {
                        ddlType.Enabled = true;
                        ddlCustomer.Enabled = true;
                        txtCustomer.Enabled = true;
                        //txtNo.Enabled = true;
                        ddlClass.Enabled = true;
                        txtRequirement.Enabled = true;
                        txtSales.Enabled = true;
                        ddlSuccess.Enabled = true;
                        txtMoldRequire.Enabled = true;
                        ddlMoldClass.Enabled = true;
                    }

                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            Baojia objBaojia = new Baojia
            {
                WorkParentId = workParentId,
                Number = no,
                Customer = ddlCustomer.SelectedValue == "其他" ? txtCustomer.Text.Trim().ToString() : ddlCustomer.SelectedValue,
                Class = ddlClass.SelectedValue,
                CostType = ddlType.SelectedValue,
                Requirement = txtRequirement.Text.Trim() == "" ? 0 : Convert.ToDouble(txtRequirement.Text.Trim()),
                Sales = txtSales.Text.Trim() == "" ? 0 : Convert.ToDouble(txtSales.Text.Trim()),
                Success = Convert.ToChar(ddlSuccess.SelectedValue)
            };
            //objBaojia.Sales = Convert.ToDouble(txtSales.Text.Trim());
            //objBaojia.Number = this.txtNo.Text;
            //objBaojia.Class = ddlClass.SelectedValue;
            try
            {
                objBaojiaManager.InsertBaojia(objBaojia);
                foreach (var item in Enum.GetValues(typeof(Customer)))
                {
                    if (objBaojia.Customer == item.ToString())
                    {
                        ddlCustomer.SelectedValue = objBaojia.Customer;
                        otherCustmoer.Style.Value = "display:none";
                        break;
                    }
                    else
                    {
                        ddlCustomer.SelectedValue = "其他";
                        txtCustomer.Text = objBaojia.Customer;
                        otherCustmoer.Style.Value = "";
                    }
                }
                string msg = string.Format("<script>alert('{0}报价编号为{1}')</script>", "保存成功! ", no);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
    }

    enum Customer
    {
        苏州萨克,
        Amphenol,
        UAES,
        TYCO,
        IRISO,
        BOSCH,
        DELPHI,
        TCS,
        SCHNEIDER
    }
}